import os
import re

from nonebot import on_regex
from nonebot.typing import T_State
from nonebot.adapters.onebot.v11 import GroupMessageEvent, Bot, MessageSegment, Message
from nonebot import on_message
from nonebot.rule import Rule




# 自定义规则函数
def not_specified_qq(event: GroupMessageEvent) -> bool:
    return event.get_user_id() != "786310583" and event.get_user_id() != "1909362660" and event.group_id != "963355029"

# 定义一个新的规则来检查消息中的文本部分是否包含 'qwq'
def contains_nihao_in_text(event: GroupMessageEvent) -> bool:
    for segment in event.message:
        if segment.type == 'text' and re.search(r'(?i)你好', segment.data.get('text', '')):
            return True
    return False

# 使用 Rule 结合自定义规则
Test_qwq = on_message(
    rule=Rule(not_specified_qq, contains_nihao_in_text),
    priority=1,
    block=False  # 如果希望这个处理器处理完后不再传递给其他处理器，可以设置为 True
)

@Test_qwq.handle()
async def qwq_detect(bot: Bot, event: GroupMessageEvent):
    # 发送文字消息
    await Test_qwq.send(message=Message("你好"))
    
    # 发送语音消息
    # await Test_qwq.send(message=Message(f"[debug]os.getcwd() = {os.getcwd()}"))
    voice_message = MessageSegment.record(os.getcwd() + '/src/plugins/qwq-bot-plugins/nihao.mp3')  # 语音文件路径
    # await Test_qwq.send(message=Message("[debug] voice"))
    await Test_qwq.send(message=voice_message)
    
    
    # # 监听所有消息，检测是否包含"你好"
# # Test_qwq = on_regex(pattern=r'^qwq$', priority=1)
# Test_nihao = on_regex(pattern=r'(?i)你好', priority=1)

# @Test_nihao.handle()
# async def nihao_detect(bot: Bot, event: GroupMessageEvent, state: T_State):
#     # 发送文字消息
#     await Test_qwq.send(message=Message("你好"))
    
#     # 发送语音消息
#     voice_message = MessageSegment.record(os.getcwd() + '/src/plugins/qwq-bot-plugins/nihao.mp3')  # 语音文件路径
#     # await Test_qwq.send(message=Message("[debug] voice"))
#     await Test_qwq.send(message=voice_message)


